<?php

namespace Lib\Model;

class UserModel {

    static function checkAuth($login, $password) {
        $b = \Lib\Database::getConnection()->prepare("
            select username, roles
            from user
            where username = :username
            and password = MD5(:password)");
        $b->bindValue("username", $login, \PDO::PARAM_STR);
        $b->bindValue("password", $password, \PDO::PARAM_STR);
        $b->execute();
        return $b->fetch(\PDO::FETCH_ASSOC);
    }

    static function checkUnique($login = NULL, $email = NULL) {
        $b = \Lib\Database::getConnection()->prepare("
            select username, email
            from user
            where username = :username
            or email = :email");
        $b->bindValue("username", $login, \PDO::PARAM_STR);
        $b->bindValue("email", $email, \PDO::PARAM_STR);
        $b->execute();
        $user = $b->fetch();
        if (isset($user['username']) && $user['username'] == $login) {
            return 1;
        }
        if (isset($user['email']) && $user['email'] == $email) {
            return 2;
        }
        return 0;
    }

    static function register($form) {
        try {
            $conn = \Lib\Database::getConnection();
            $conn->beginTransaction();
            $b = $conn->prepare("
            insert
            into user(roles, username, password, email)
            values(:roles,:username,:password,:email)");
            $b->bindValue("roles", 'ROLE_USER', \PDO::PARAM_STR);
            $b->bindValue("username", $form['username'], \PDO::PARAM_STR);
            $b->bindValue("password", MD5($form['password']), \PDO::PARAM_STR);
            $b->bindValue("email", $form['email'], \PDO::PARAM_STR);
            $b->execute();
            $userId = $conn->lastInsertId();
            $b = $conn->prepare("
            insert
            into user_ext(user_id)
            values(:userId)");
            $b->bindValue("userId", $userId, \PDO::PARAM_INT);
            $b->execute();
            $conn->commit();
            return TRUE;
        } catch (\Exception $e) {
            $conn->rollBack();
            return FALSE;
        }
    }
    
    static function update($userId, $form) {
        try {
            $conn = \Lib\Database::getConnection();
            $conn->beginTransaction();
            $b = $conn->prepare("
            update 
            user_ext ue
            set ue.first_name = :first_name, 
            ue.last_name = :last_name, 
            ue.`status` = :status, 
            ue.birth_date = :birth_date, 
            ue.gender = :gender
            where user_id = :userId");
            $b->bindValue("first_name", $form['first_name'], \PDO::PARAM_STR);
            $b->bindValue("last_name", $form['last_name'], \PDO::PARAM_STR);
            $b->bindValue("status", $form['status'], \PDO::PARAM_INT);
            $b->bindValue("birth_date", $form['birth_date'], \PDO::PARAM_STR);
            $b->bindValue("gender", $form['gender'], \PDO::PARAM_INT);
            $b->bindValue("userId", $userId, \PDO::PARAM_INT);
            $b->execute();
            $conn->commit();
            return TRUE;
        } catch (\Exception $e) {
            $conn->rollBack();
            return FALSE;
        }
    }
    
    static function getByName($username){
        $b = \Lib\Database::getConnection()->prepare("
            select u.id, u.username, u.email, ue.first_name, ue.last_name, ue.status, ue.logo, ue.birth_date, ue.gender
            from user u
            join user_ext ue on u.id = ue.user_id
            where username = :username
            limit 1");
        $b->bindValue("username", $username, \PDO::PARAM_STR);
        $b->execute();
        return $b->fetch(\PDO::FETCH_ASSOC);
    }

    static function isAuth(){
         if(isset($_SESSION['user']) && !empty($_SESSION['user'])){
             return TRUE;
         } else{
             return FALSE;
         }
    }
    
    static function getUser(){
         if(isset($_SESSION['user']) && !empty($_SESSION['user'])){
             return $_SESSION['user'];
         } else{
             return FALSE;
         }
    }
    
    static function isAdmin(){
         if(isset($_SESSION['roles']) && $_SESSION['roles'] == 'ROLE_ADMIN'){
             return TRUE;
         } else{
             return FALSE;
         }
    }
}

?>
